﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using l1cs.server.templates;
using l1cs.server.serverpackets;
using l1cs.server.datatables;
using System.Threading;

namespace l1cs.server.model.Instance
{
    public class L1QuestInstance : L1NpcInstance
    {
        private static Logger _log = Logger.getLogger( typeof( L1QuestInstance ).FullName );

        private L1QuestInstance _npc;

        public L1QuestInstance( L1Npc template )
            : base( template )
        {
            _npc = this;
        }

        override public void onNpcAI()
        {
            int npcId = getNpcTemplate().get_npcId();
            if ( isAiRunning() )
            {
                return;
            }
            if ( npcId == 71075 || npcId == 70957 || npcId == 81209 )
            {
                return;
            }
            else
            {
                setActived( false );
                startAI();
            }
        }

        override public void onAction( L1PcInstance pc )
        {
            L1Attack attack = new L1Attack( pc, this );
            if ( attack.calcHit() )
            {
                attack.calcDamage();
                attack.calcStaffOfMana();
                attack.addPcPoisonAttack( pc, this );
                attack.addChaserAttack();
            }
            attack.action();
            attack.commit();
        }

        override public void onTalkAction( L1PcInstance pc )
        {
            int pcX = pc.getX();
            int pcY = pc.getY();
            int npcX = getX();
            int npcY = getY();

            if ( pcX == npcX && pcY < npcY )
            {
                setHeading( 0 );
            }
            else if ( pcX > npcX && pcY < npcY )
            {
                setHeading( 1 );
            }
            else if ( pcX > npcX && pcY == npcY )
            {
                setHeading( 2 );
            }
            else if ( pcX > npcX && pcY > npcY )
            {
                setHeading( 3 );
            }
            else if ( pcX == npcX && pcY > npcY )
            {
                setHeading( 4 );
            }
            else if ( pcX < npcX && pcY > npcY )
            {
                setHeading( 5 );
            }
            else if ( pcX < npcX && pcY == npcY )
            {
                setHeading( 6 );
            }
            else if ( pcX < npcX && pcY < npcY )
            {
                setHeading( 7 );
            }
            broadcastPacket( new S_ChangeHeading( this ) );

            int npcId = getNpcTemplate().get_npcId();
            if ( npcId == 71092 || npcId == 71093 )
            { // 調査員
                if ( pc.isKnight() && pc.getQuest().get_step( 3 ) == 4 )
                {
                    pc.sendPackets( new S_NPCTalkReturn( getId(), "searcherk1" ) );
                }
                else
                {
                    pc.sendPackets( new S_NPCTalkReturn( getId(), "searcherk4" ) );
                }
            }
            else if ( npcId == 71094 )
            { // エンディア
                if ( pc.isDarkelf() && pc.getQuest().get_step( 4 ) == 1 )
                {
                    pc.sendPackets( new S_NPCTalkReturn( getId(), "endiaq1" ) );
                }
                else
                {
                    pc.sendPackets( new S_NPCTalkReturn( getId(), "endiaq4" ) );
                }
            }
            else if ( npcId == 71062 )
            { // カミット
                if ( pc.getQuest().get_step( L1Quest.QUEST_CADMUS )
                        == 2 )
                {
                    pc.sendPackets( new S_NPCTalkReturn( getId(), "kamit1b" ) );
                }
                else
                {
                    pc.sendPackets( new S_NPCTalkReturn( getId(), "kamit1" ) );
                }
            }
            else if ( npcId == 71075 )
            { // 疲れ果てたリザードマンファイター
                if ( pc.getQuest().get_step( L1Quest.QUEST_LIZARD )
                        == 1 )
                {
                    pc.sendPackets( new S_NPCTalkReturn( getId(), "llizard1b" ) );
                }
                else
                {
                    pc.sendPackets( new S_NPCTalkReturn( getId(), "llizard1a" ) );
                }
            }
            else if ( npcId == 70957 || npcId == 81209 )
            { // ロイ
                if ( pc.getQuest().get_step( L1Quest.QUEST_ROI )
                        != 1 )
                {
                    pc.sendPackets( new S_NPCTalkReturn( getId(), "roi1" ) );
                }
                else
                {
                    pc.sendPackets( new S_NPCTalkReturn( getId(), "roi2" ) );
                }
            }

            lock ( this )
            {
                if ( _monitor != null )
                {
                    _monitor.Dispose();
                }
                setRest( true );
                //_monitor = new RestMonitor();
                //_restTimer.schedule( _monitor, REST_MILLISEC );
                _monitor = new Timer( new TimerCallback( RestMonitorTimerSub ), null, REST_MILLISEC, Timeout.Infinite );
            }
        }

        override public void onFinalAction( L1PcInstance pc, String action )
        {
            if ( string.Compare( action, "start", true ) == 0 )
            {
                int npcId = getNpcTemplate().get_npcId();
                if ( ( npcId == 71092 || npcId == 71093 )
                        && pc.isKnight() && pc.getQuest().get_step( 3 ) == 4 )
                {
                    L1Npc l1npc = NpcTable.getInstance().getTemplate( 71093 );
                    L1FollowerInstance follow = new L1FollowerInstance( l1npc, this, pc );
                    pc.sendPackets( new S_NPCTalkReturn( getId(), "" ) );
                }
                else if ( npcId == 71094
                      && pc.isDarkelf() && pc.getQuest().get_step( 4 ) == 1 )
                {
                    L1Npc l1npc = NpcTable.getInstance().getTemplate( 71094 );
                    L1FollowerInstance follow = new L1FollowerInstance( l1npc, this, pc );
                    pc.sendPackets( new S_NPCTalkReturn( getId(), "" ) );
                }
                else if ( npcId == 71062
                      && pc.getQuest().get_step( L1Quest.QUEST_CADMUS ) == 2 )
                {
                    L1Npc l1npc = NpcTable.getInstance().getTemplate( 71062 );
                    L1FollowerInstance follow = new L1FollowerInstance( l1npc, this, pc );
                    pc.sendPackets( new S_NPCTalkReturn( getId(), "" ) );
                }
                else if ( npcId == 71075
                      && pc.getQuest().get_step( L1Quest.QUEST_LIZARD ) == 1 )
                {
                    L1Npc l1npc = NpcTable.getInstance().getTemplate( 71075 );
                    L1FollowerInstance follow = new L1FollowerInstance( l1npc, this, pc );
                    pc.sendPackets( new S_NPCTalkReturn( getId(), "" ) );
                }
                else if ( npcId == 70957 || npcId == 81209 )
                {

                    L1Npc l1npc = NpcTable.getInstance().getTemplate( 70957 );
                    L1FollowerInstance follow = new L1FollowerInstance( l1npc, this, pc );
                    pc.sendPackets( new S_NPCTalkReturn( getId(), "" ) );
                }
            }
        }

        private const long REST_MILLISEC = 10000;

        //private static final Timer _restTimer = new Timer(true);

        //private RestMonitor _monitor;
        private Timer _monitor;

        //public class RestMonitor extends TimerTask {
        //    override
        public void RestMonitorTimerSub( Object state )
        {
            setRest( false );
        }
        //}

    }

}
